$.namespace("monitor.common");

monitor.common = function () {
    var function_, interval;
    return {
        init: function () {
            this.disable();
            this.buildFooter();
        },
        buildHeadAndInitInterval: function (index, f) {
            function_ = f;
            $.get('header.html', function (html) {
                $(document.body).prepend(html);
                $(".nav-tabs .nav-item ").eq(index).addClass("active");
                $("[data-toggle='tooltip']").tooltip();
                function_();
                monitor.common.initInterval();
            }, "html");
        },
        resetIntervalTime: function () {
            var element = $("#change_load_");
            if (!$(element).hasClass("running")) {
                $(element).addClass("running").removeClass("btn-success").addClass("btn-danger").html("停止");
                $(element).attr("title", "停止刷新数据").tooltip('fixTitle').tooltip('show');
            }
            monitor.common.initInterval();
        },
        initInterval: function () {
            clearInterval(interval);
            interval = setInterval(function_, parseInt($("#intervalTime").val()) * 1000);
        },
        changeLoadEvent: function (element) {
            if ($(element).hasClass("running")) {
                clearInterval(interval);
                $(element).removeClass("running").removeClass("btn-danger").addClass("btn-success").html("启动");
                $(element).attr("title", "开始刷新数据").tooltip('fixTitle').tooltip('show');
            } else {
                monitor.common.initInterval();
                $(element).addClass("running").removeClass("btn-success").addClass("btn-danger").html("停止");
                $(element).attr("title", "停止刷新数据").tooltip('fixTitle').tooltip('show');
            }
        },
        buildHead: function (index) {
            $.get('header.html', function (html) {
                $(document.body).prepend(html);
                $(".nav-tabs .nav-item ").eq(index).addClass("active");
                $("[data-toggle='tooltip']").tooltip();
            }, "html");
        },
        buildFooter: function () {
            var html = '<footer class="footer navbar-fixed-bottom">' +
                '    		<div class="container">' +
                '	powered by <a href="https://gitee.com/laiyw/monitor" target="_blank">Laiyw</a>' +
                '			</div>' +
                ' </footer>';
            $(document.body).append(html);
        },
        disable: function () {
            document.oncontextmenu = function () {return false;}
            document.onselectstart = function () {return false;}
            document.oncopy = function(){return false;}
        }
    }
}();

$(document).ready(function () {
    monitor.common.init();
});